package com.liudachu.interview.bishi;

/**
 * 58同城笔试题
 */
public class Inter58 {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * 将数组从指定位置拆分后交替重组。
     * @param arr int整型一维数组 输入数组
     * @param i int整型 切分偏移
     * @return int整型一维数组
     */
    public int[] shuffle (int[] arr, int i) {
        // write code here

        int[] pro = new int[i + 1];
        int[] suf = new int[arr.length-i - 1];
        for (int j = 0; j <= i; j++) {
            pro[j]=arr[j];
        }
        int count=0;
        for (int j = i+1; j <= arr.length ; j++) {
            suf[count]=arr[j];
        }

        int[] ints = new int[arr.length];
        int k=0;
        for (int j = 0; j < ints.length; j=j+2) {
            ints[j]=pro[k];
            ints[j+1]=suf[k];
            k++;
        }
        return ints;
    }


    public static String decodeString (String str) {
        // write code here
        StringBuffer stringBuffer = new StringBuffer();
        //str="3[a]2[bc]";
        char[] chars = str.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (chars[i]=='['){
                //得到对应的字符串
                StringBuilder sb=new StringBuilder();
                int k=i+1;
                while (chars[k]!=']'){
                    sb.append(chars[k]);
                    k++;
                }
                System.out.println(sb.toString());
                //遍历对应次数
                int i1 = Integer.parseInt(chars[i - 1]+"");
                for (int j = 0; j <i1; j++) {
                    stringBuffer.append(sb);
                }
            }
        }
        return stringBuffer.toString();
    }


    public static int calculateExtraNum (int totalNum, int exchangeNum) {
        // write code here
        int more=0;
        int k=0;
        for (int i = 0; i <totalNum ; i++) {
            if (k==exchangeNum){
                more++;
                k=0;
                totalNum++;
            }else{
                k++;
            }
            System.out.println("111");
        }
        return more;
    }


    public static int StringSplit (String str) {
        // write code here
        StringBuffer stringBuffer = new StringBuffer(str);
        int count=0;
        int max=0;
        for (int i = 1; i < stringBuffer.length(); i++) {
            String l = stringBuffer.substring(0, i);
            String f = stringBuffer.substring(i, stringBuffer.length());
            count = getA(l) + getB(f);
            if (count>max){
                max=count;
            }
        }
        return max;

    }

    public static int getA (String str) {
        // write code here
        char[] chars = str.toCharArray();
        int count=0;
        for (char aChar : chars) {
            if (aChar=='A'){
                count++;
            }
        }
        return count;
    }

    public static int getB (String str) {
        // write code here
        char[] chars = str.toCharArray();
        int count=0;
        for (char aChar : chars) {
            if (aChar=='B'){
                count++;
            }
        }
        return count;
    }

    public static void main(String[] args) {
        StringSplit("abbbab");
    }

}
